<?php
class Admin extends MY_Controller{
	
	public function index(){
                $index=$this->uri->segment(3);
                $list['list']=$this->paging($index);
		$this->load->view('include/header');
		$this->load->view('body/admin',$list);
	}
    public function upload(){
    	if (!empty($_FILES)) {
    		$targetPath = getcwd().Utils::get_image_path();
    		$fileTypes = array('image/jpg','image/jpeg','image/gif','image/png');     	
    		if (in_array($_FILES["file_upload"]['type'],$fileTypes)) {
    			move_uploaded_file($_FILES["file_upload"]['tmp_name'],$targetPath.$_FILES["file_upload"]['name']);
    			$title=$this->input->get_post('title');
    			$content=$this->input->get_post('content');
    			$data=array('title'=>$title,'image_alias'=>$_FILES["file_upload"]['name'],'content'=>$content,'status'=>1);
    			$this->save_image($data);
    		} else {
    			echo 'Invalid file type.';
    		}
    	}
    	 
    }	
    
    /*
     * @param $data 数据信息:title,image ,content
     */
    private function save_image($data){
    	$this->db->insert('carts',$data);
    }
	
    private function carts_list($start,$end){
    	$query=$this->db->query('select * from carts where status=1 limit '.$start. ','. $end);
        return $this->query_result_to_array($query);
    }
    private function paging($index){
         $config['base_url'] = base_url().'/admin/index/'; //导入分页类URL
         $config['total_rows'] = $this->db->count_all('carts');  //计算总记录数
         $config['per_page'] = 9;         //每页显示的记录数
         $config['full_tag_open'] = '<p>';
         $config['full_tag_close'] = '</p>';
         $this->pagination->initialize($config);      //初始化分类页
         if($index==null){
              return $this->carts_list(0,$config['per_page']);
		}else{
              return $this->carts_list($index,$config['per_page']);   
	}


 	 }
}
